package com.itheima.reggic.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggic.common.BaseContext;
import com.itheima.reggic.common.R;
import com.itheima.reggic.pojo.Employee;
import com.itheima.reggic.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);

        return R.success(employee);
    }

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        String username = employee.getUsername();
        String password = employee.getPassword();

        // 查询用户是否存在
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

        if (null == emp) {
            return R.error("登录失败");
        }

        // 判断密码是否正确
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!emp.getPassword().equals(password)) {
            return R.error("登录失败");
        }

        // 判断用户是否被禁用
        if (emp.getStatus() == 0) {
            return R.error("账户已被禁用");
        }

        // 以上条件都不满足，则登录成功
        // 设置cookie信息
        request.getSession().setAttribute("employee", emp.getId());

        // 将信息放到线程内
//        BaseContext.setCurrentId(emp.getId());

        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");

        return R.success("退出成功!");
    }

    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        Page<Employee> page_obj = new Page<>(page, pageSize);


        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);

        queryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(page_obj, queryWrapper);

        return R.success(page_obj);

    }

    @PostMapping
    public R<String> add(HttpServletRequest request, @RequestBody Employee employee) {
        // 设置默认参数
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        // 设置创建人和更新人
        //Long employeeId = (Long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(employeeId);
        //employee.setUpdateUser(employeeId);
        boolean save = employeeService.save(employee);
        if (save)
            return R.success("添加成功");
        else
            return R.error("添加失败");
    }

    @PutMapping
    public R<String> chageStatus(HttpServletRequest request, @RequestBody Employee employee) {
        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("修改成功！");
    }
}
